<?php

namespace iBrand\Shop\Backend\Http\Controllers;

use Encore\Admin\Facades\Admin as LaravelAdmin;
use Encore\Admin\Layout\Content;
use iBrand\Component\Goods\Repositories\GoodsRepository;
use iBrand\Component\Purchase\Models\Purchase;
use iBrand\Component\Purchase\Repositories\PurchaseRepository;
use iBrand\Component\Goods\Models\Supplier;
use iBrand\Shop\Backend\Service\CommonService;

class PurchaseRejectController extends Controller
{
	protected $purchaseRepository;
	protected $goodsRepository;
	protected $commonService;

	public function __construct(PurchaseRepository $purchaseRepository,
	                            GoodsRepository $goodsRepository,
	                            CommonService $commonService)
	{
		$this->purchaseRepository = $purchaseRepository;
		$this->goodsRepository    = $goodsRepository;
		$this->commonService      = $commonService;
	}

	public function index()
	{
		$condition = $this->searchCondition();

		$lists = $this->purchaseRepository->getPurchasePaginate($condition[0], $condition[1]);

		return LaravelAdmin::content(function (Content $content) use ($lists) {

			$content->header('采购退货单');

			$content->breadcrumb(
				['text' => '采购管理', 'url' => 'warehouse/purchase', 'no-pjax' => 1],
				['text' => '采购退货单', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '采购退货单']
			);

			$content->body(view('shop-backend::purchaseReject.index', compact('lists')));
		});
	}

	private function searchCondition()
	{
		$where['shop_id'] = session('shop_id');
		$where['type']    = Purchase::TYPE_REJECT;
		$time             = [];

		if (request('code')) {
			$where['code'] = ['like', '%' . request('code') . '%'];
		}

		if (!empty(request('etime')) && !empty(request('stime'))) {
			$where['created_at'] = ['<=', request('etime')];
			$time['created_at']  = ['>=', request('stime')];
		}

		if (!empty(request('etime'))) {
			$time['created_at'] = ['<=', request('etime')];
		}

		if (!empty(request('stime'))) {
			$time['created_at'] = ['>=', request('stime')];
		}

		return [$where, $time];
	}

	public function show()
	{
		$order_no       = request('order_no');
		$purchaseReject = $this->purchaseRepository->findWhere(['code' => $order_no])->first();

		return LaravelAdmin::content(function (Content $content) use ($purchaseReject) {

			$content->header('查看采购退货详情');

			$content->breadcrumb(
				['text' => '采购管理', 'url' => 'warehouse/purchase', 'no-pjax' => 1],
				['text' => '采购退货单', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '采购退货单']
			);

			$content->body(view('shop-backend::purchaseReject.show', compact('purchaseReject')));
		});
	}

	public function create()
	{
		$suppliers   = Supplier::where('status', 1)->get(['id', 'name', 'code'])->toArray();
		$specList    = $this->commonService->getSpecList();
		$colors      = $specList['colors'];
		$sizes       = $specList['sizes'];
		$order_type  = Purchase::TYPE_REJECT;
		$button_text = '采购退货';

		return LaravelAdmin::content(function (Content $content) use ($suppliers, $colors, $sizes, $order_type, $button_text) {

			$content->header('采购退货单');

			$content->breadcrumb(
				['text' => '采购管理', 'url' => 'warehouse/purchase', 'no-pjax' => 1],
				['text' => '采购退货单', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '采购退货单']
			);

			$content->body(view('shop-backend::purchase.create', compact('suppliers', 'sizes', 'colors', 'order_type', 'button_text')));
		});
	}
}